package com.linkwechat.controller;

import com.linkwechat.common.utils.ResultUtil;
import com.linkwechat.common.vo.CommonRsp;
import com.linkwechat.common.vo.qw.GetVisitUserVO;
import com.linkwechat.domain.wecom.vo.ConfigSignatureVO;
import com.linkwechat.domain.wecom.vo.department.QwDeptListVO;
import com.linkwechat.domain.wecom.vo.user.QwDeptUserVO;
import com.linkwechat.service.QwService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 王君平
 * @Date 2024/6/8 16:24
 * @Description
 */
@Slf4j
@Api(value = "企业微信微信-sys-api", tags = {"企业微信-api"})
@RequestMapping("/qywx/sys")
@Controller
public class QwController {

    @Autowired
    private QwService qwService;

    @ApiOperation(value = "accessToken 自建应用的accesstoken",hidden = false)
    @RequestMapping(value = "/accessToken", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<String> accessToken(
            @RequestParam(value = "corpId", required = true) String corpId,
            @RequestParam(value = "corpSecret", required = true) String corpSecret
    ) {
        String token = qwService.accessToken(corpId,corpSecret);
        return ResultUtil.success(token);
    }


    @ApiOperation(value = "通讯录accessToken 自建应用的通讯录accesstoken",hidden = false)
    @RequestMapping(value = "/contactAccessToken", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<String> contactAccessToken(
            @RequestParam(value = "corpId", required = true) String corpId,
            @RequestParam(value = "contactSecret", required = true) String contactSecret
    ) {
        String token = qwService.contactAccessToken(corpId,contactSecret);
        return ResultUtil.success(token);
    }

    @ApiOperation(value = "获取用户访问身份",hidden = false)
    @RequestMapping(value = "/loginAuthVisitUser", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<GetVisitUserVO> loginAuthVisitUser(
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "accessToken", required = true) String accessToken
    ) {
        GetVisitUserVO vo = qwService.getLoginAuthVisitUser(code,accessToken);
        return ResultUtil.success(vo);
    }

    @ApiOperation(value = "获取部门id列表",hidden = false)
    @RequestMapping(value = "/deptList", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<QwDeptListVO> deptList(
            @RequestParam(value = "accessToken", required = true) String accessToken,
            @RequestParam(value = "id", required = false) String id
    ) {
        QwDeptListVO vo = qwService.deptList(accessToken,id);
        return ResultUtil.success(vo);
    }

    /**
     * 需要先开启  https://work.weixin.qq.com/wework_admin/frame#/apps/contactsApi
     * @param contactAccessToken
     * @param nextCursor
     * @return
     */
    @ApiOperation(value = "获取用户id列表",hidden = false)
    @RequestMapping(value = "/deptUser", method = RequestMethod.GET)
    @ResponseBody
    CommonRsp<QwDeptUserVO> deptUser(
            @RequestParam(value = "contactAccessToken", required = true) String contactAccessToken,
            @RequestParam(value = "nextCursor", required = false) String nextCursor
    ){
        QwDeptUserVO vo = qwService.deptUser(contactAccessToken,nextCursor);
        return ResultUtil.success(vo);
    }

    /**https://developer.work.weixin.qq.com/document/24364  往下翻
     * 获取 获取企业 jsapi_ticket
     * @return
     */
    @RequestMapping(value = "/getJsapiTicket", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<String> getJsapiTicket(
            @RequestParam(value = "accessToken", required = true) String accessToken
    ) {
        String jssdkTicket = qwService.getJsapiTicket(accessToken);
        return ResultUtil.success(jssdkTicket);
    }

    /**
     * https://developer.work.weixin.qq.com/document/24364
     * jsjsdk用  企业的
     */
    @ApiOperation(value = "jsjsdk用",hidden = false)
    @RequestMapping(value = "/getConfigSignature", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<ConfigSignatureVO> getConfigSignature(
            @RequestParam(value = "jsapiTicket", required = true) String jsapiTicket,
            @RequestParam(value = "url", required = true) String url
    ) {
        ConfigSignatureVO vo = qwService.getConfigSignature(jsapiTicket,url);
        return ResultUtil.success(vo);
    }

    /**
     * 应用的 jsapi_ticket
     * @return
     */
    @RequestMapping(value = "/getAgentJsapiTicket", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<String> getAgentJsapiTicket(
            @RequestParam(value = "accessToken", required = true) String accessToken,
            @RequestParam(value = "corpId", required = true) String corpId
    ) {
        String jssdkTicket = qwService.getAgentJsapiTicket(accessToken,corpId);
        return ResultUtil.success(jssdkTicket);
    }

    /**
     *
     * jsjsdk的签名 应用的
     */
    @ApiOperation(value = "jsjsdk用",hidden = false)
    @RequestMapping(value = "/getAgentConfigSignature", method = RequestMethod.GET)
    @ResponseBody
    public CommonRsp<ConfigSignatureVO> getAgentConfigSignature(
            @RequestParam(value = "jsapiTicket", required = true) String jsapiTicket,
            @RequestParam(value = "url", required = true) String url
    ) {
        ConfigSignatureVO vo = qwService.getAgentConfigSignature(jsapiTicket,url);
        return ResultUtil.success(vo);
    }

}
